using ProtoModels;
using UnityEngine;

public class LeaderboardEntry : MonoBehaviour
{
	public UILabel rankLabel;

	public UILabel starLabel;

	public UILabel nameLabel;

	public UILabel rankNameLabel;

	public UILabel score;

	public UISprite flag;

	public UISprite bck;

	public UISprite highlight;

	public UISprite rankBck;

	public float fadeColorToBlack = 0.5f;

	public UITexture avatarTexture;

	private GGServerRequestsBackend.TextureRequest textureRequest;

	private Color playerColour = new Color(0f, 0.714f, 0.22f, 1f);

	private Color opponentColour = new Color(0.122f, 0.223f, 0.31f, 1f);

	private string playerSprite = "dialog-bck-down";

	private string opponentSprite = "dialog-bck";

	public void InitEntry(Lead.Leaderboard.Ranking ranking, bool isPlayer, Color activeColor)
	{
		UITools.ChangeText(rankLabel, string.Empty + (ranking.leaderboardIndex + 1) + ".");
		UITools.ChangeText(starLabel, ranking.stars);
		string[] array = ranking.name.Split(' ');
		UITools.ChangeText(nameLabel, (array.Length <= 0) ? ranking.name : array[0]);
		if (rankBck != null)
		{
			rankBck.color = Color.Lerp(activeColor, Color.black, fadeColorToBlack);
		}
		int result = 0;
		if (int.TryParse(ranking.score, out result))
		{
			UITools.ChangeText(score, GGFormat.FormatPrice(result));
		}
		int result2 = 0;
		if (int.TryParse(ranking.stars, out result2))
		{
			UITools.ChangeText(rankNameLabel, GGPlayerSettings.instance.GetDisplayEloRating(result2).ToString());
		}
		if (bck != null)
		{
			bck.color = ((!isPlayer) ? opponentColour : playerColour);
			UITools.ChangeSprite(bck, (!isPlayer) ? opponentSprite : playerSprite);
		}
		if (textureRequest == null || textureRequest.url != ranking.imageUrl)
		{
			avatarTexture.gameObject.SetActive(false);
			flag.gameObject.SetActive(true);
			GameConstants.SetFlag(flag, ranking.country);
			if (GGPlayerSettings.instance.useProfilePictureInMultiplayer && ranking.imageUrl != string.Empty)
			{
				textureRequest = new GGServerRequestsBackend.TextureRequest(ranking.imageUrl);
				textureRequest.cache = CacheStategy.GetFromCache;
				textureRequest.onComplete = OnRequestComplete;
				BehaviourSingleton<GGRequestLimitter>.instance.Add(textureRequest);
			}
		}
		UITools.SetActive(highlight, isPlayer);
	}

	public void OnRequestComplete(GGServerRequestsBackend.ServerRequest request)
	{
		GGServerRequestsBackend.TextureRequest textureRequest = request as GGServerRequestsBackend.TextureRequest;
		if (textureRequest != null && textureRequest == this.textureRequest)
		{
			if (request.status != 0)
			{
				this.textureRequest = null;
				return;
			}
			avatarTexture.gameObject.SetActive(true);
			flag.gameObject.SetActive(false);
			avatarTexture.mainTexture = textureRequest.response;
		}
	}
}
